// pages/community/community/index.js
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';

let pre_community_type = 0;
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type_value: pre_community_type,
    type_option: [{
      value: 0,
      text: "全部帖子",
    }],
    type_all: {
      value: 0,
      text: "全部帖子",
    },
    overlayShow: false,
    searchKey: "",
    communitySearchKey: "",
    resultUrl: "../community/index",
    posts: [
      {
        "id": 1,
        "userID": "thm",
        "plogTypeID": 1,
        "imagePath": "../../../image/go.png",
        "creatTime": "2022-03-08T10:47:04",
        "plogName": "天气",
        "plogContent": "今天天气真好"
      },
      {
        "id": 2,
        "userID": "thm",
        "plogTypeID": 2,
        "imagePath": "d",
        "creatTime": "2022-03-08T10:51:43",
        "plogName": "运动",
        "plogContent": "今天跑了100米"
      }
    ],
  },
  /**
   * 显示搜索
   */
  showPopup() {
    this.setData({
      overlayShow: true
    })
  },
  /**
   * 获取帖子分类
   */
  getPlogType: function () {
    var that = this
    var tempType = that.data.type_option;

    wx.request({
      url: app.globalData.url + "/plog_type",
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (result) {
        console.log("getPlogType:result.data")
        console.log(result.data)
        if (result.statusCode == 200) {
          for (var index in result.data) {
            var plogType = {};
            plogType.value = result.data[index].id
            plogType.text = result.data[index].typeName
            tempType.push(plogType)
          }
          that.setData({
            type_option: tempType
          })
          console.log("getPlogType:result.type_option:")
          console.log(that.data.type_option)
        } else {
          Toast.fail("获取帖子分类失败,状态码：" + result.data.statusCode)
        }
      },
      fail: () => {

        Toast.fail("连接失败")
      },
      complete: () => {
        console.log("GetPlogType request complete")
      }
    });
  },
  /**
   * 获取全部帖子
   */
  getAllPlogs: function () {
    var that = this;
    wx.request({
      url:  app.globalData.url + "/get_plog/",
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (result) {
        console.log(result)
        if (result.statusCode == 200) {
          that.setData({
            posts: result.data
          })
        } else {
          Toast.fail("获取全部帖子失败,状态码：" + result.data.statusCode)
        }
      },
      fail: () => { Toast.fail("连接失败") },
      complete: () => { }
    });
  },
  /**
   * 按分类搜索帖子 
   * @param { int } value 
   */
  onChageType: function (value) {
    console.log("value.detail");
    console.log(value.detail);
    pre_community_type = value.detail
    var that = this;
    if (value.detail == 0) {
      that.getAllPlogs()
    } else {
      wx.request({
        url: app.globalData.url + "/categorized_plog/",
        data: { plogTypeID: value.detail },
        header: { 'content-type': 'application/json' },
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: function (result) {
          console.log("onChageType:result.data" + result.data)
          if (result.statusCode == 200) {
            that.setData({
              posts: result.data
            })
          } else {
            Toast.fail("获取" + value.detail + "类帖子失败,状态码：" + result.statusCode)
          }
        },
        fail: () => { Toast.fail("连接失败") },
        complete: () => { }
      });
    }
  },
  
  /**
   * 按关键词搜索帖子
   */
  onSearch: function (e) {
    wx.navigateTo({ 
      url: "../results/index?searchKey=" + e.detail
    })
  },

  /**
   * 取消搜索
   */
  onCancel: function () {
    this.setData({ communitySearchKey: "" });
    // Toast('取消' + this.data.communitySearchKey);
    this.setData({ overlayShow: false });
  },
  onSearchIconClick: function () {
    this.setData({ overlayShow: true });
  },
  /**
   * 跳转至详情
   */
  toDetailsTap: function (e) {
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: "../detail/index?id=" + e.currentTarget.dataset.id
    })
  },
  /**
   * 下拉刷新
   * @param {int} type 
   */
  onRefresh: function (type) {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    if (type == 0) {
      that.getAllPlogs()
    } else {
      console.log("onChageType:" + type)
      wx.request({
        url: app.globalData.url + '/categorized_plog/',
        data: { plogTypeID: type },
        header: { 'content-type': 'application/json' },
        method: 'GET',
        dataType: 'json',
        responseType: 'text',
        success: function (result) {
          if (result.statusCode == 200) {
            that.setData({
              posts: result.data
            })
          } else {
            Toast.fail("获取" + type + "类帖子失败,状态码：" + result.statusCode)
          }
        },
        fail: () => { Toast.fail("连接失败") },
        complete: () => { }
      });
    }
    setTimeout(function () {
      wx.hideLoading()
      wx.stopPullDownRefresh()
    }, 2000)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getPlogType();
    that.getAllPlogs()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("onPullDownRefresh")
    console.log(pre_community_type)
    this.onRefresh(pre_community_type)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    Toast("没有更多帖子啦")
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})